package dfs;

import java.util.*;

/**
 * @author pengfei.hpf
 * @date 2020/2/12
 * @verdion 1.0.0
 * Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2.
 *
 *  
 *
 * Example:
 *
 * Input: [4, 6, 7, 7]
 * Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]
 *  
 *
 * Note:
 *
 * The length of the given array will not exceed 15.
 * The range of integer in the given array is [-100,100].
 * The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence.
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/increasing-subsequences
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class FindSubsequences {

    public List<List<Integer>> findSubsequences(int[] nums) {
        Set<List<Integer>> res = new HashSet<>();
        if(nums == null || nums.length < 2){
            return new ArrayList<>();
        }
        Map<Integer, Set<List<Integer>>> map = new HashMap<>();
        for(int i = nums.length - 1; i >=0; i --){
            Set<List<Integer>> curList = new HashSet<>();
            List<Integer> cur = new ArrayList<>();
            cur.add(nums[i]);
            curList.add(cur);
            for(Integer pre: map.keySet()){
                if(pre >= nums[i]){
                    Set<List<Integer>> lists= map.get(pre);
                    for(List<Integer> l: lists){
                        List<Integer> newList = new ArrayList<>();
                        newList.add(nums[i]);
                        newList.addAll(l);
                        res.add(newList);
                        curList.add(newList);
                    }
                }
            }
            map.put(nums[i], curList);
        }
        return new ArrayList<>(res);
    }
}
